package com.lzy.mall.order.web;

import com.lzy.mall.order.service.OmsOrderService;
import com.lzy.mall.order.vo.OrderConfirmVo;
import com.lzy.mall.order.vo.OrderSubmitVo;
import com.lzy.mall.order.vo.SubmitOrderResponseVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.ExecutionException;

/**
 * @author lzy
 * @date 2021/10/3 14:41
 */
@Controller
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class OrderWeb {

    private final OmsOrderService orderService;

    @GetMapping("/toTrade")
    public String toTrade(Model model, HttpServletRequest request) {
        try {
            request.getSession();
        } catch (Exception exception) {
            log.info("HttpServletRequest错误");
        }
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData", orderConfirmVo);
        System.out.println("提交订单数据：" + orderConfirmVo);
        return "confirm";
    }

    /**
     * 下单功能
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo, Model model) {

        log.info("下单！！！！！！！！！！！！！！！");
        try {
            SubmitOrderResponseVo responseVo = orderService.submitOrder(vo);
            log.info("submitOrder(vo)：{}",responseVo);
            // 下单失败回到订单重新确认订单信息
            if (responseVo.getCode() == 0) {
                // 下单成功取支付选项
                model.addAttribute("submitOrderResp", responseVo);
                return "pay";
            } else {
                log.info("else");
                return "redirect:http://order.lzyan.com/toTrade";
            }
        } catch (Exception exception) {
            log.info("e:{}",exception);
            return "redirect:http://order.lzyan.com/toTrade";
        }
    }
}
